package com.xiaobao.coalproperty.module.find.view;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.find.model.ForumDetailEntity;
import com.xiaobao.coalproperty.module.home.model.entity.BlankEntity;
import com.xiaobao.coalproperty.other.adapter.CommentAdapter;
import com.xiaobao.coalproperty.other.base.BaseActivity;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.DialogUtils;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;

public class TopicDetailsActivity extends BaseActivity {


    @BindView(R.id.topic_details_recycler)
    XRecyclerView mXRecyclerView;
    @BindView(R.id.topic_details_comment)
    ImageView btnComment;

    private View headView;
    private CircleImageView ivHead;
    private TextView tvName, tvTime, tvTitle, tvConent, tvCancel;
    private EditText edtConent;
    private ImageView ivSubmit;
    private CommentAdapter mCommentAdapter;
    private String id;

    private View popupView;
    private PopupWindow popupWindow;

    @Override
    protected int getContentView() {
        return R.layout.activity_topic_details;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setLeftClickFinish();
        setTitle("帖子详情");

        id = getIntent().getStringExtra(getIntent().getAction());

        headView = LayoutInflater.from(this).inflate(R.layout.item_adapter_topic_details_head, null, false);
        ivHead = headView.findViewById(R.id.topic_details_head);
        tvName = headView.findViewById(R.id.topic_details_name);
        tvTime = headView.findViewById(R.id.topic_details_time);
        tvTitle = headView.findViewById(R.id.topic_details_title);
        tvConent = headView.findViewById(R.id.topic_details_content);

        mCommentAdapter = new CommentAdapter(this);

        mXRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mXRecyclerView.addHeaderView(headView);
        mXRecyclerView.setLoadingListener(mLoadingListener);
        mXRecyclerView.setAdapter(mCommentAdapter);

        updateTopicDetail();
    }

    private XRecyclerView.LoadingListener mLoadingListener = new XRecyclerView.LoadingListener() {
        @Override
        public void onRefresh() {
            updateTopicDetail();
            mXRecyclerView.refreshComplete();
        }

        @Override
        public void onLoadMore() {
            mXRecyclerView.loadMoreComplete();
        }
    };

    public void commentClick(View view) {
        showPopupwindow();
    }

    private void updateTopicDetail() {
        OkHttpUtils.post().url(UrlConstant.URL_FORUMDETAIL)
                .params(HttpHelperUtils.getInstance().forumDetailParams(id))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    ForumDetailEntity entity = new Gson().fromJson(response, ForumDetailEntity.class);
                    if (entity.getCode() == 0) {
                        updateTopicDetailSuccess(entity);
                        return;
                    }
                    showToast(entity.getMsg());
                } catch (Exception e) {
                }
            }
        });
    }

    private void updateTopicDetailSuccess(ForumDetailEntity entity) {
        Glide.with(this).load(entity.getData().getAvatar()).into(ivHead);
        tvName.setText(entity.getData().getNickname());
        tvTime.setText(entity.getData().getDate());
        tvTitle.setText(entity.getData().getTitle());
        tvConent.setText(entity.getData().getContent());
        mCommentAdapter.setAdapterData(entity.getData().getComment());
    }

    @SuppressLint({"NewApi"})
    private void showPopupwindow() {
        popupView = LinearLayout.inflate(this, R.layout.popupwindow_comment, null);
        tvCancel = popupView.findViewById(R.id.comment_cancel);
        edtConent = popupView.findViewById(R.id.comment_content);
        ivSubmit = popupView.findViewById(R.id.comment_submit);
        ivSubmit.setOnClickListener(submitOnClickListener);
        tvCancel.setOnClickListener(cancelOnClickListener);
        popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setOnDismissListener(onDismissListener);
        popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
        setScreenBgDarken();
    }

    private PopupWindow.OnDismissListener onDismissListener = new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            setScreenBgLight();
        }
    };

    private View.OnClickListener cancelOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            popupWindow.dismiss();
        }
    };

    private View.OnClickListener submitOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(TextUtils.isEmpty(edtConent.getText())){
                showToast("评论不能为空");
                return;
            }
            addForumComment();
        }
    };

    private void addForumComment(){
        OkHttpUtils.post().url(UrlConstant.URL_ADD_FORUM_COMMENT)
                .params(HttpHelperUtils.getInstance().addForumCommentParams(id,edtConent.getText().toString()))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    BlankEntity entity = new Gson().fromJson(response,BlankEntity.class);
                    if(entity.getCode()==0){
                        popupWindow.dismiss();
                        updateTopicDetail();
                        return;
                    }
                    showToast(entity.getMsg());
                }catch (Exception e){}
            }
        });
    }

    // 设置屏幕背景变暗
    private void setScreenBgDarken() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 0.5f;
        lp.dimAmount = 0.5f;
        getWindow().setAttributes(lp);
    }

    // 设置屏幕背景变亮
    private void setScreenBgLight() {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 1.0f;
        lp.dimAmount = 1.0f;
        getWindow().setAttributes(lp);
    }


}
